﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace VirtualDisk
{
    class CdCommand : Command
    {
        private MyString[] arguments = null;

        private void processArgument(out MyString path)
        {
            path = DiskTree.getInstance().getPWDPath();
            if (arguments.Length != 0)
            {
                path = arguments[0].Trim();
            }
        }
        protected override int run()
        {
            MyString path;
            processArgument(out path);
            if (!DiskTree.getInstance().isExist(path))
            {
                return 1;
            }

            if (!DiskTree.getInstance().isADirctory(path))
            {
                return 2;
            }

            DiskTree.getInstance().changePWD(path);
            return 0;
        }

        protected override void processError(int returnCode)
        {
            switch (returnCode)
            {
                case 1:
                    Console.WriteLine("系统找不到指定的路径!");
                    break;
                case 2:
                    Console.WriteLine("目录名无效!");
                    break;
                default:
                    break;
            }
        }

        public CdCommand(MyString[] argu)
        {
            if (argu != null)
            {
                arguments = new MyString[argu.Length];
                for (int i = 0; i < arguments.Length; i++)
                {
                    arguments[i] = argu[i];
                }
            }

        }
    }
}
